<?php
/**
 2016-07-06
 楠岀绫?
*/
namespace Think;

class	encrypt{
   
  


/**
	 * 浣跨敤绌烘牸鏇挎崲鎹㈣绗?
	 * @param string string_before
	 * @return string string_after
	 */
	 function string_replace($string_before){
		$string_after = str_replace('%','PP',$string_before);
        $string_after = str_replace('&','AND',$string_after);
        $string_after = str_replace(',','',$string_after);
		return $string_after;
	 }


  /**
   楠岀
   data锛歶tf-8缂栫爜鐨勮鍗曞師鏂?
   */
function sign($data) {
    //璇诲彇绉侀挜鏂囦欢
	//娉ㄦ剰鎵€鏀炬枃浠惰矾寰?
	$pem_url = "http://".$_SERVER['HTTP_HOST']."/310440300024993_prv.pem";
    $priKey = file_get_contents($pem_url);	
 
    //杞崲涓簅penssl瀵嗛挜锛屽繀椤绘槸娌℃湁缁忚繃pkcs8杞崲鐨勭閽?
    $res = openssl_get_privatekey($priKey);
	
	//print_r($data);die;
 
    //璋冪敤openssl鍐呯疆绛惧悕鏂规硶锛岀敓鎴愮鍚?sign
    openssl_sign($data, $sign, $res);
 
    //閲婃斁璧勬簮
    openssl_free_key($res);
	
//	echo "<meta charset='gbk'>";
//	print_r(base64_encode($sign));die;
 
    return base64_encode($sign);
}



	/**
     鍗佸叚杩涘埗楠岀
	*/
	 function binsign($data){
		 //璇佷功璺緞
		 $privatekeyFile = "http://".$_SERVER['HTTP_HOST']."/310440300024993.pfx";
		 //璇佷功绉侀挜
		 $passphrase="885154618780";
		 $signature = '';   
		 $privateKey;
		 $signedMsg;
		 $pkcs12 = file_get_contents($privatekeyFile);
	 
		if (openssl_pkcs12_read($pkcs12, $certs, "885154618780")) {
		   $privateKey = $certs['pkey'];
		}
		if (openssl_sign($data, $signedMsg, $privateKey,OPENSSL_ALGO_SHA1)) {
		   $signedMsg= strtoupper(bin2hex($signedMsg));//杩欎釜鐪嬫儏鍐点€傛湁浜涗笉闇€瑕佽浆鎹㈡垚16杩涘埗锛屾湁浜涢渶瑕乥ase64缂栫爜銆傜湅鍚勪釜鎺ュ彛
		   return $signedMsg;
		} 
		 
	 }

   function pubkeyEncrypt($data,$panText,$pubkey){
      openssl_public_encrypt($data,$panText,$pubkey,OPENSSL_PKCS1_PADDING);
      return	strtoupper(bin2hex($panText));
   
   }
   
  function getBytes($string) {  
      $bytes = array();  
      for($i = 0; $i < strlen($string); $i++){  
        $bytes[] = ord($string[$i]);  
      }  
      return $bytes;  
   }  
   }
 

?>